/**
 * Created by cyf on 15-12-1.
 * 全局对象
 */
//服务器地址
global.serverPath = "127.0.0.1";
//服务器端口
global.serverPort = "9999";
//视频信号转发器
global.rtcSignalMaster = "https://sandbox.simplewebrtc.com:443/";
//当前登录用户名
global.currentUserLoginName = null;
//当前登录用户名
global.currentUserName = null;
//聊天窗口集合
global.chatWinArray = new Array();
//聊天窗口集合
global.groupChatWinArray = new Array();
//websocket操作对象
global.webSocketHandler = null;
//聊天窗口操作对象
global.chartWinHandler = null;
//群组聊天窗口操作对象
global.groupChatWinHandler = null;
//群组管理窗口操作对象
global.groupInfoWinHandler = null;
//界面对象
global.mainIndex = null;
//托盘对象
global.tray = null;
//全院人员树
global.orgTreeObj = null;
//最近联系人树
global.recentContactsDataTreeObj = null;
//个人好友分组树
global.personalBandDataTreeObj = null;
//群组树
global.groupDataTreeObj = null;
//通知模块
global.nwNotify = require('nw-notify');
global.gui = require('nw.gui');
//初始化
$(function() {
	//创建托盘
	global.tray = new qxchat.Tray();
	//创建websocket操作对象
	global.webSocketHandler = new qxchat.QXWebSocket();
	//初始化聊天窗口操作对象
	global.chartWinHandler = new qxchat.ChartWinHandler();
	//初始化群组聊天窗口操作对象
	global.groupChatWinHandler = new qxchat.GroupChartWinHandler();
	//初始化群组管理窗口操作对象
	global.groupInfoWinHandler = new qxchat.GroupInfoWinHandler();
	//构建界面
	global.mainIndex = new qxchat.Index();
	$('#loginBtn').bind('click', function() {
		global.serverPath = $('#g_ServerPath').val();
		global.serverPort = $('#g_ServerPort').val();
		global.rtcSignalMaster = "ws://" + global.serverPath + ":" + $('#g_ServerVideoPort').val();
		if (global.serverPath == null || global.serverPath == '') {
			alert("请设置服务器地址！")
			return;
		}
		if (global.serverPort == null || global.serverPort == '') {
			alert("请设置服务器端口！")
			return;
		}
		if ($('#loginName').val() == null || $('#loginName').val() == '') {
			alert("请输入用户名！")
			return;
		}
		if ($('#password').val() == null || $('#password').val() == '') {
			alert("请输入密码！")
			return;
		}
		// 信息存入本地
		localStorage.setItem("g_ServerPath", $('#g_ServerPath').val());
		localStorage.setItem("g_ServerPort", $('#g_ServerPort').val());
		localStorage.setItem("g_ServerVideoPort", $('#g_ServerVideoPort').val());
		localStorage.setItem("loginName", $('#loginName').val());
		localStorage.setItem("password", $('#password').val());

		localStorage.setItem("isRemember", $('#isRemember').is(':checked'));
		localStorage.setItem("isAutoLogin", $('#isAutoLogin').is(':checked'));

		global.currentUserLoginName = $('#loginName').val();
		layer.load(0, {
			shade: false
		});
		//创建连接
		global.webSocketHandler.connect();
	});
	//初始读取信息
	var _tmp_serverPath = localStorage.getItem("g_ServerPath");
	var _tmp_serverPort = localStorage.getItem("g_ServerPort");
	var _tmp_serverVideoPort = localStorage.getItem("g_ServerVideoPort");
	var _tmp_loginName = localStorage.getItem("loginName");
	var _tmp_password = localStorage.getItem("password");
	var _tmp_isRemember = localStorage.getItem("isRemember");
	var _tmp_isAutoLogin = localStorage.getItem("isAutoLogin");

	if (_tmp_serverPath != null && _tmp_serverPath != '') {
		$('#g_ServerPath').val(_tmp_serverPath);
	}
	if (_tmp_serverPort != null && _tmp_serverPort != '') {
		$('#g_ServerPort').val(_tmp_serverPort);
	}
	if (_tmp_serverVideoPort != null && _tmp_serverVideoPort != '') {
		$('#g_ServerVideoPort').val(_tmp_serverVideoPort);
	}
	if (_tmp_isRemember != null && _tmp_isRemember != '') {
		if (_tmp_isRemember == 'true') {
			$('#loginName').val(_tmp_loginName);
			$('#password').val(_tmp_password);
			$('#isRemember').attr("checked", 'true');
		}
	}
	if (_tmp_isAutoLogin != null && _tmp_isAutoLogin != '') {
		if (_tmp_isAutoLogin == 'true') {
			$('#isAutoLogin').attr("checked", 'true');
			$('#loginBtn').trigger("click");
		}
	}
});